/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.its.dsw2.view;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import mx.its.dsw2.biblioteca.bl.LibroBlLocal;
import mx.its.dsw2.biblioteca.bl.PrestamoBlLocal;
import mx.its.dsw2.biblioteca.bl.UsuarioBlLocal;
import mx.its.dsw2.biblioteca.modelo.DetallePrestamo;
import mx.its.dsw2.biblioteca.modelo.Libro;
import mx.its.dsw2.biblioteca.modelo.Prestamo;

/**
 *
 * @author 100101
 */
@ManagedBean
@SessionScoped
public class LoanBean {
    @EJB
    private LibroBlLocal libroBl;
    
    @EJB
    private UsuarioBlLocal usuarioBl;
    @EJB
    private PrestamoBlLocal prestamoBl;
    
    
    private List<Prestamo> listLoan;
    private List<DetallePrestamo> listDetailLoanTemporary;
    private List<Libro> listBooks;
    private String accion="";
    private Prestamo loan;
    private DetallePrestamo detailLoan;
    private int idUsuario;
    private int idLibro;
    /**
     * Creates a new instance of LoanBean
     */
    public LoanBean() {
        loan=new Prestamo();
        detailLoan=new DetallePrestamo();
    }

    public Prestamo getLoan() {
        return loan;
    }

    public void setLoan(Prestamo loan) {
        this.loan = loan;
    }

    public DetallePrestamo getDetailLoan() {
        return detailLoan;
    }

    public void setDetailLoan(DetallePrestamo detailLoan) {
        this.detailLoan = detailLoan;
    }
    
    

    public List<Prestamo> getListLoan() {
        listLoan=prestamoBl.getAll();
        return listLoan;
    }

    public List<DetallePrestamo> getListDetailLoanTemporary() {
        return listDetailLoanTemporary;
    }

    public void setListDetailLoanTemporary(List<DetallePrestamo> listDetailLoanTemporary) {
        this.listDetailLoanTemporary = listDetailLoanTemporary;
    }

    public List<Libro> getListBooks() {
        return listBooks;
    }
    
    

    public int getIdUsuario() {
        return idUsuario;
    }

    public void setIdUsuario(int idUsuario) {
        this.idUsuario = idUsuario;
    }

    public int getIdLibro() {
        return idLibro;
    }

    public void setIdLibro(int idLibro) {
        this.idLibro = idLibro;
    }
    
    
    
    
    public void prepararNuevo(ActionEvent evt){
        accion="Nuevo";
        loan=new Prestamo();
        detailLoan=new DetallePrestamo();
//        listDetailLoanTemporary= new ArrayList<DetallePrestamo>();
        listBooks=new ArrayList<Libro>();
    }
    
    public void prepararEditar(ActionEvent evt){
        accion="Editar";
        int index=Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
        loan=listLoan.get(index);
    }
    
    public void prepararEliminar(ActionEvent evt){
        accion="Eliminar";
        int index=Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
        loan=listLoan.get(index);
    }
    
    public void eliminarLibroDeLista(ActionEvent evt){//createedit
//      accion="Eliminar2";
               int index=Integer.parseInt(evt.getComponent().getClientId().split(":")[3]);
//       category=listCategory.get(index);  
       listBooks.remove(index);
       
   }
    
    public void agregarLibroALista(){
       listBooks.add(libroBl.getLibroPorId(idLibro));
    }
    public String procesarAccion(){
        if(accion.equalsIgnoreCase("Nuevo")){
            prestamoBl.register(loan,listBooks);  
        }
        accion="";
        return "LoanList";
    }
    
  
    
    public String llamadaPaginas(){
         String link="";
         if(accion.equalsIgnoreCase("Editar")||accion.equalsIgnoreCase("Nuevo")){
             link="LoanCreateEdit";
         }else{
             if(accion.equalsIgnoreCase("Eliminar")){
                 link="LoanList";
             }
             }
         
         return link;
     }
    
    public String accionAceptar(){
       loan.setIdUsuario(usuarioBl.getUserById(idUsuario));
       return "LoanConfirm";
   }
    
}
